package com.zhang.controller;

import com.alibaba.fastjson.JSON;
import com.zhang.client.ProductFeignClient;
import com.zhang.entity.BaseCategoryView;
import com.zhang.entity.ProductSalePropertyKey;
import com.zhang.entity.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
public class WebDetailController {
    @Autowired
    private ThreadPoolExecutor myExecutor;
    @Autowired
    private ProductFeignClient productFeignClient;

    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        Map<String, Object> dataMap = new HashMap<>();

        /***
         * 创建线程池在这里创建 但是这里创建只会该程序使用 我们把创建线程池
         * 封装到一个配置类里面 以后直接使用 shop_common_utils
         */

        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            //c.根据skuId查询商品的实时价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            dataMap.put("price", skuPrice);
        },myExecutor);

        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //a.根据skuId查询商品的基本信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            dataMap.put("skuInfo", skuInfo);
            return skuInfo;
        },myExecutor);
        CompletableFuture<Void> categoryViewFuture = skuInfoFuture.thenAcceptAsync((SkuInfo skuInfo) -> {
            Long category3Id = skuInfo.getCategory3Id();
            //b.根据三级分类id获取商品的分类信息
            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            dataMap.put("categoryView", categoryView);
        },myExecutor);
        CompletableFuture<Void> spuSalePropertyFuture = skuInfoFuture.thenAcceptAsync((SkuInfo skuInfo) -> {
            //d.获取该SKU对应的销售属性(一份)和所有的销售属性(全份)
            Long productId = skuInfo.getProductId();
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSalePropertyAndSelected(productId, skuId);
            dataMap.put("spuSalePropertyList",spuSalePropertyList);
        },myExecutor);

        CompletableFuture<Void> salePropertyValueFuture = skuInfoFuture.thenAcceptAsync((SkuInfo skuInfo) -> {
            //d.获取该SKU对应的销售属性(一份)和所有的销售属性(全份)
            Long productId = skuInfo.getProductId();
            //e.销售属性组合与SkuId的对应关系
            Map<Object, Object> salePropertyAndSkuIdMapping = productFeignClient.getSalePropertyAndSkuIdMapping(productId);
            dataMap.put("salePropertyValueIdJson", JSON.toJSONString(salePropertyAndSkuIdMapping));
        },myExecutor);
        //应该让所有任务执行完了之后 才返回结果
        CompletableFuture.allOf(priceFuture,
                skuInfoFuture,
                categoryViewFuture,
                spuSalePropertyFuture,
                salePropertyValueFuture).join();
        //model.addAttribute("salePropertyValueIdJson", JSON.toJSONString(salePropertyAndSkuIdMapping));
        //注意 注意 注意  不要写成model.addAttribute()
        model.addAllAttributes(dataMap);
        return "detail/index";
    }


/*    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        Map<String, Object> dataMap = new HashMap<>();
        //a.根据skuId查询商品的基本信息
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        dataMap.put("skuInfo",skuInfo);
//        model.addAttribute("skuInfo",skuInfo);
        Long category3Id = skuInfo.getCategory3Id();
        //b.根据三级分类id获取商品的分类信息
        BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
        dataMap.put("categoryView",categoryView);
//        model.addAttribute("categoryView",categoryView);
        //c.根据skuId查询商品的实时价格
        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
        dataMap.put("price",skuPrice);
//        model.addAttribute("price",skuPrice);
        //d.获取该SKU对应的销售属性(一份)和所有的销售属性(全份)
        Long productId = skuInfo.getProductId();
        List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSalePropertyAndSelected(productId, skuId);
        dataMap.put("spuSalePropertyList",spuSalePropertyList);
//        model.addAttribute("spuSalePropertyList",spuSalePropertyList);
        //e.销售属性组合与SkuId的对应关系
        Map<Object, Object> salePropertyAndSkuIdMapping = productFeignClient.getSalePropertyAndSkuIdMapping(productId);
        dataMap.put("salePropertyValueIdJson", JSON.toJSONString(salePropertyAndSkuIdMapping));
        //model.addAttribute("salePropertyValueIdJson", JSON.toJSONString(salePropertyAndSkuIdMapping));
        //注意 注意 注意  不要写成model.addAttribute()
        model.addAllAttributes(dataMap);
        return "detail/index";
    }*/
}
